<template>
  <!-- 图片详情 -->
  <view>
    <!-- 用户信息 -->
    <view class="user-info">
      <view class="user-avatar">
        <image :src="imgDetail.user.avatar" mode="widthFix"></image>
      </view>
      <view class="user-desc">
        <view class="user-name">{{imgDetail.user.name}}</view>
        <view class="user-time">{{imgDetail.previousTime}}</view>
      </view>
    </view>
    <!-- 高清大图 -->
    <swiper-action @swiperaction="handleSwiperAction">
      <view class="high-img">
        <image :src="imgDetail.thumb" mode="widthFix"></image>
      </view>
    </swiper-action>
    <!-- 点赞，收藏 -->
    <view class="user-like">
      <view class="like">
        <text
          class="iconfont icondianzan"></text><text>{{imgDetail.rank}}</text>
      </view>
      <view class="collect">
        <text class="iconfont iconshoucang"></text><text>收藏</text>
      </view>
    </view>
    <!-- 专辑 -->
    <view class="album-wrap" v-if="album.length>0">
      <view class="relative">相关</view>
      <view class="album-list">
        <view class="album-item" v-for="item in album" :key="item.id">
          <view class="album-cover">
            <image :src="item.cover" mode="aspectFill"></image>
          </view>
          <view class="album-info">
            <view class="album-info-text">专辑</view>
            <view class="album-name">{{item.name}}</view>
            <text class="iconfont iconiconfontjiantou4"></text>
          </view>
        </view>
      </view>
    </view>

    <!-- 最热评论 -->
    <view class="comment hot" v-if="hot.length>0">
      <view class="comment-title">
        <text class="iconfont iconhot1"></text>
        <text class="comment-text">最热评论</text>
      </view>
      <view class="comment-list">
        <view class="comment-item" v-for="item in hot" :key="item.id">
          <!-- 用户信息 -->
          <view class="user-info">
            <!-- 用户头像 -->
            <view class="user-avatar">
              <image :src="item.user.avatar" mode="widthFix"></image>
            </view>
            <!-- 用户名称和评论时间 -->
            <view class="user-name-wrap">
              <view class="user-name">{{item.user.name}}</view>
              <view class="user-time">{{item.cnTime}}</view>
            </view>
          </view>
          <!-- 用户徽章 -->
          <view class="user-badge">
            <image v-for="item2 in item.user.title" :key="item2.icon"
              :src="item2.icon" mode="widthFix"></image>
          </view>
          <!-- 评论信息 -->
          <view class="comment-desc">
            <view class="comment-content">{{item.content}}</view>
            <view class="comment-like">
              <text class="iconfont icondianzan">{{item.size}}</text>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 最新评论 -->
    <view class="comment new" v-if="comment.length>0">
      <view class="comment-title">
        <text class="iconfont iconpinglun"></text>
        <text class="comment-text">最热评论</text>
      </view>
      <view class="comment-list">
        <view class="comment-item" v-for="item in comment" :key="item.id">
          <!-- 用户信息 -->
          <view class="user-info">
            <!-- 用户头像 -->
            <view class="user-avatar">
              <image :src="item.user.avatar" mode="widthFix"></image>
            </view>
            <!-- 用户名称和评论时间 -->
            <view class="user-name-wrap">
              <view class="user-name">{{item.user.name}}</view>
              <view class="user-time">{{item.cnTime}}</view>
            </view>
          </view>
          <!-- 用户徽章 -->
          <view class="user-badge">
            <image v-for="item2 in item.user.title" :key="item2.icon"
              :src="item2.icon" mode="widthFix"></image>
          </view>
          <!-- 评论信息 -->
          <view class="comment-desc">
            <view class="comment-content">{{item.content}}</view>
            <view class="comment-like">
              <text class="iconfont icondianzan">{{item.size}}</text>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 下载 -->
    <view class="downlaod">
      <view class="download-btn" @click="handleDownload">下载图片</view>
    </view>
  </view>
</template>

<script>
import swiperAction from '@/components/SwiperAction'
import moment from 'moment'
// 设置moment的语言为中文
moment.locale('zh-cn')
export default {
  data() {
    return {
      // 图片信息对象
      imgDetail: {},
      // 专辑数据
      album: [],
      // 最热评论
      hot: [],
      // 最新评论
      comment: [],
      // 图片的索引
      imgIndex: 0
    }
  },
  components: {
    swiperAction
  },
  onLoad() {
    const { imgIndex } = getApp().globalData
    this.imgIndex = imgIndex
    this.getData()
  },
  methods: {
    // 给imgDetail赋值
    getData() {
      const { imgList } = getApp().globalData
      this.imgDetail = imgList[this.imgIndex]

      // 将时间戳转为 多久前 格式
      if (this.imgDetail !== undefined) {
        this.imgDetail.previousTime = moment(
          this.imgDetail.atime * 1000
        ).fromNow()
        this.getComments(this.imgDetail.id)
      }
    },
    // 获取评论数据
    async getComments(id) {
      const res = await this.request({
        url: `http://157.122.54.189:9088/image/v2/wallpaper/wallpaper/${id}/comment`
      })
      this.album = res.res.album
      // 将hot数组元素对象添加一个 时间属性：多久前
      res.res.hot.forEach(v => {
        v.cnTime = moment(v.atime * 1000).fromNow()
      })
      res.res.comment.forEach(v => {
        v.cnTime = moment(v.atime * 1000).fromNow()
      })
      this.hot = res.res.hot
      this.comment = res.res.comment
    },
    // 手指左右滑动事件
    handleSwiperAction(params) {
      // 判断 imgIndex 是否超出数组范围
      // params.direction为left，imgIndex++，
      // params.direction === "left" && this.imgIndex < imgList.length-1
      // params.direction为right，imgIndex--
      // params.direction === "right" && this.imgIndex > 0
      const { imgList } = getApp().globalData
      if (params.direction === 'left' && this.imgIndex < imgList.length - 1) {
        this.imgIndex++
        this.getData()
      } else if (params.direction === 'right' && this.imgIndex > 0) {
        this.imgIndex--
        this.getData()
      } else {
        uni.showToast({
          title: '没有更多了~',
          icon: 'none'
        })
      }
    },
    // 点击下载图片
    async handleDownload() {
      await uni.showLoading({
        title: '下载中'
      })

      // 将远程文件下载到小程序内存中
      const res1 = await uni.downloadFile({
        url: this.imgDetail.img
      })
      const { tempFilePath } = res1[1]

      // 将小程序内存中的临时文件下载到本地上，电脑的话会弹窗询问下载路径，手机的话会自动下载到手机系统的相册
      const res2 = await uni.saveImageToPhotosAlbum({ filePath: tempFilePath })
      uni.hideLoading()
      // 成功下载后
      if (res2[1]) {
        await uni.showToast({
          title: '下载成功',
          icon: 'none'
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
// 用户信息
.user-info {
  display: flex;
  align-items: center;
  padding: 20rpx;
  .user-avatar {
    padding: 0 20rpx;
    image {
      width: 88rpx;
      border-radius: 50%;
    }
  }
  .user-desc {
    .user-name {
      color: #000;
      font-weight: 600;
    }

    .user-time {
      padding: 10rpx 0;
      color: #aaa;
      font-size: 24rpx;
    }
  }
}
.high-img {
  border-bottom: 2rpx solid #eee;
}
// 点赞，收藏
.user-like {
  display: flex;
  height: 80rpx;
  line-height: 80rpx;
  border-bottom: 3rpx solid #eee;
  .like {
    flex: 1;
    text-align: center;
    border-right: 1rpx solid #eee;
    .iconfont {
      margin-right: 5rpx;
    }
  }

  .collect {
    flex: 1;
    text-align: center;
    border-left: 1rpx solid #eee;
    .iconfont {
      position: relative;
      top: 2rpx;
      margin-right: 5rpx;
    }
  }
}
// 相关专辑
.album-wrap {
  padding: 20rpx;
  .relative {
    padding: 10rpx 0;
  }

  .album-list {
    .album-item {
      display: flex;
      padding: 10rpx 0;
      border-bottom: 10rpx solid #eee;
      .album-cover {
        flex: 1;
        image {
          width: 180rpx;
          height: 180rpx;
        }
      }

      .album-info {
        position: relative;
        flex: 3;
        padding-left: 20rpx;
        .album-info-text {
          width: 100rpx;
          height: 50rpx;
          line-height: 50rpx;
          text-align: center;
          color: #fff;
          background-color: $theme-color;
        }

        .album-name {
          padding: 10rpx 0;
          color: #888;
        }

        .iconfont {
          position: absolute;
          top: 50%;
          right: 10%;
          transform: translateY(-50%);
          font-size: 40rpx;
          color: #333;
        }
      }
    }
  }
}
// 最热，最新评论
.comment {
  .comment-title {
    padding: 15rpx;
    .iconfont {
      color: red;
      font-size: 40rpx;
    }

    .comment-text {
      font-weight: 600;
      font-size: 28rpx;
      color: #666;
      margin-left: 10rpx;
    }
  }

  .comment-list {
    .comment-item {
      position: relative;
      border-bottom: 15rpx solid #eee;
      // 用户信息
      .user-info {
        display: flex;
        padding: 20rpx 0;
        .user-avatar {
          width: 15%;
          display: flex;
          justify-content: center;
          align-items: center;
        }

        .user-name-wrap {
          flex: 1;
          .user-name {
            color: #777;
          }

          .user-time {
            color: #aaa;
            font-size: 23rpx;
            padding: 5rpx 0;
          }
        }
      }

      .user-badge {
        position: absolute;
        top: 30rpx;
        right: 10rpx;
        image {
          float: right;
          margin: 0 2rpx;
          width: 40rpx;
          height: 40rpx;
        }
      }

      .comment-desc {
        display: flex;
        align-items: center;
        padding: 10rpx 0 30rpx 15%;
        .comment-content {
          flex: 1;
          color: #000;
        }

        .comment-like {
          margin-right: 20rpx;
          text-align: right;
        }
      }
    }
  }
}
.new .iconpinglun {
  color: aqua !important;
}
.downlaod {
  display: flex;
  justify-content: center;
  align-items: center;
  .download-btn {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 90%;
    padding: 15rpx 0;
    color: #fff;
    font-size: 36rpx;
    font-weight: 550;
    background-color: $theme-color;
  }
}
</style>